package com.hiwan.unions.activity.main.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.loginRegister.LoginUserActivity;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;
import com.hiwan.unions.activity.system.AccountActivity;
import com.hiwan.unions.activity.system.newfragment.NewsActivity;
import com.hiwan.unions.activity.system.SetActivity;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.StartIntent;
import com.hiwan.unions.util.Utils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

/**
 * Created by llg on 2017/6/7.
 */
public class MyFragment extends RootFragment implements View.OnClickListener, HttpListener<JSONObject> {
    private TextView my_name, number_yh, number_hd, text_jifen;
    private ImageView image_logo;
    private Button image_coment;
    private MainActivity activity;
    Request<JSONObject> request;

    @Override
    public int getLayout() {
        canLoadMore = false;
        canRefresh = false;
        return R.layout.fragment_my;
    }

    public void local() {
        if (CheckUtils.userInfo(userInfo)) {
            request = NoHttp.createJsonObjectRequest(ConstsNet.home, RequestMethod.POST);
            CallServer.getRequestInstance().addToken(getActivity(), 2, request, this, false, userInfo);

            image_coment.setVisibility(View.VISIBLE);
            image_logo.setImageResource(R.mipmap.touxiang_yes);
            try {
                my_name.setText(CheckUtils.checkName(userInfo));
            } catch (ApiException e) {
                my_name.setText(e.getMessage());
            }
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void Interface() {
        super.Interface();
        activity = (MainActivity) getActivity();
        my_name = findView(R.id.my_name);
        number_yh = findView(R.id.number_yh);
        number_hd = findView(R.id.number_hd);
        text_jifen = findView(R.id.text_jifen);
        image_coment = findView(R.id.image_coment);
        image_coment.setOnClickListener(this);
        image_logo = findView(R.id.image_logo);
        image_logo.setOnClickListener(this);
        findView(R.id.image_news).setOnClickListener(this);
        findView(R.id.image_qiandao).setOnClickListener(this);
        findView(R.id.huiji_layout).setOnClickListener(this);
        findView(R.id.zhanghu_layout).setOnClickListener(this);
        findView(R.id.xt_layout).setOnClickListener(this);
        findView(R.id.gy_layout).setOnClickListener(this);
        findView(R.id.youhui_layout).setOnClickListener(this);
        findView(R.id.huod_layout).setOnClickListener(this);
        findView(R.id.ll_jifen).setOnClickListener(this);

        // 当SDK版本号为19以上版本时
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            findView(R.id.RelaButtonTitlenear).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
        } else {
            local();
        }
    }

    @Override
    public void onClick(View v) {
        if (CheckUtils.userInfo(userInfo)) {
            switch (v.getId()) {
                case R.id.image_logo:
                    break;
                case R.id.xt_layout:
                    startActivity(new Intent(getActivity(), SetActivity.class));
                    break;
                case R.id.gy_layout:
                    request = NoHttp.createJsonObjectRequest(ConstsNet.about, RequestMethod.POST);
                    request.add("folder", "about_us");
                    CallServer.getRequestInstance().addToken(getActivity(), 1, request, this, true, userInfo);
                    break;
                case R.id.image_coment:
                    request = NoHttp.createJsonObjectRequest(ConstsNet.loginout, RequestMethod.POST);
                    CallServer.getRequestInstance().addToken(getActivity(), 0, request, this, true, userInfo);
                    break;
                default:
                    butoon(v.getId());
                    break;
            }
        } else {
            LoginUserActivity.startPreview2(getActivity());
        }
    }

    private void butoon(int i) {
        switch (i) {
            case R.id.image_news:
                startActivity(new Intent(getActivity(), NewsActivity.class));
                break;
            case R.id.image_qiandao:
                Utils.callNoTitle(ConstsNet.QIANDAO, getActivity(), "");
                break;
            case R.id.ll_jifen:
                Utils.callNoTitle(ConstsNet.QIANDAO, getActivity(), "");
                break;
            case R.id.zhanghu_layout:
                startActivity(new Intent(getActivity(), AccountActivity.class));
                break;
            case R.id.youhui_layout:
//                startActivity(new Intent(getActivity(), CouponActivity.class));
                StartIntent.get(getActivity()).contentStart("我的优惠券");
                break;
            case R.id.huod_layout:
//                startActivity(new Intent(getActivity(), EventActivity.class));
//                WebViewDetailedActivity.
                StartIntent.get(getActivity()).contentStart("我的活动");
                break;
            case R.id.huiji_layout:
                StartIntent.get(getActivity()).contentStart("会籍信息");
                break;


        }
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        switch (what) {
            case 0:
                SharedPreferences.Editor editor = userInfo.edit();
                editor.clear();
                editor.putBoolean(ConstantsValues.IS_HASLOGIN, false);
                editor.commit();
                activity.setDimss();
                break;
            case 1:
                jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
                Bundle bundle = new Bundle();
                bundle.putString("method", "");
                bundle.putBoolean("urlType", true);
                bundle.putString("url", Utils.callGanyu(jsonObject));
                Utils.callHelpDeilsActivity(mContext, bundle);
                break;
            case 2:
                jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
                number_yh.setText(JsonUtil.getString(jsonObject, "myPrize"));
                number_hd.setText(JsonUtil.getString(jsonObject, "myactiviti"));
                text_jifen.setText(JsonUtil.getString(jsonObject, "myPoints"));
                SharedPreferences.Editor editor2 = userInfo.edit();
                editor2.putString(ConstantsValues.name, JsonUtil.getString(jsonObject, "name"));
                editor2.commit();
                try {
                    my_name.setText(CheckUtils.checkName(userInfo));
                } catch (ApiException e) {
                    my_name.setText(e.getMessage());
                }
                break;
        }

    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}
